package de.gwtprime.shared.util;

import java.util.Random;

import de.gwtprime.shared.requestFactory.ArticleSectionProxy;

public class AppUtilsShared {
	
	/**
	 * Don'T use automatic trading
	 */
	public static boolean useSimpleTrading = true;
	
	/**
	 * The minimum unit that can be traded
	 */
	public static Double minTradingUnit = 0.1;
	
	
	public static String websiteTitle = "Litecoin Börse";
	
	public static String mailSenderAdress = "janpumpe@googlemail.com";
	
	/**
	 * Minimum length of the username
	 */
	public static int minimumUserNameLength = 3;
	
	/**
	 * Maxmimum length of the username
	 */
	public static int maximumUserNameLength = 255;
	
	
	/**
	 * Minimum length of the username
	 */
	public static int minimumPasswordLength = 6;
	
	/**
	 * Maxmimum length of the username
	 */
	public static int maximumPasswordLength = 255;
	
	
	
	/**
	 * Enable the next previous button on the article page
	 * -> Maybe slows down page load
	 */
	public final static boolean enableNextPrevArticleBtn = true;
	
	/**
	 * Hash fragment
	 */
	public final static String hashFragment = "#!";
	
	/**
	 * Pattern for validating email addresses
	 */
	public final static String EMAIL_PATTERN = 
			"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
					+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	
	/**
	 * Hash fragment
	 */
	public final static String hashFragmentEscaped = "_escaped_fragment_=";
	
	public final static String metaDescription = "Handelsplattform für den Handel mit Litecoins.";
			//+ "Um eine hohe Skalierbarkeit und Performance zu erreichen, wird die Anwendung als Cloudanwendung für Googles App Engine erstellt. Es werden alle wichtigen Bestandteile des Toolkits wie Widgets, UiBinder, Activities und Places, Internationalisierung vorgestellt. Aber nicht nur die Grundlagen des Toolkits sollen vorgestellt werden. Es wird der kompletten Entwicklungsprozess einer GWT Anwendung inklusive komplexerer Themen, wie beispielsweise die Anbindung einer Datenbank (Hibernate), die Search Engine Optimization oder ein Facebook/Google+ Teilen-Button für GWT Inhalte.";
	
	public final static String appTitle = "Litecoin Börse - Kaufe und Verkaufe Litecoins";
	
	/**
	 * With trailing "/"
	 */
	public final static String DOMAIN_PROD = "http://www.litecoin-boerse.appspot.com/"; 
	public final static String DOMAIN_LOCAL = "http://127.0.0.1:8888/"; 
	
	public final static String facebookId = "195397143988845";
	
	/**
	 * Facebook links are marked with fb, e.g.: /fb/ArticleSection/1 -> This marker is used to serve
	 * a special output to the facebook parser with some meta data
	 */
	public final static String FACEBOOK_MARKER = "fb"; 
	
	/**
	 * "/fb/ArticleSection/1" -> #!ArticleSection:1
	 * @param url
	 * @return
	 */
	public static String convertToGWTToken(String url){
		
		String[] splittedUrl = url.split("/");
		
		if(splittedUrl != null && splittedUrl.length < 3){
			return null;
		}
		if(splittedUrl[1].equals(FACEBOOK_MARKER)){
			
			String object = splittedUrl[2];
			String id = splittedUrl[3];
			
			return "#!"+object+":"+id;
			
		}
		return null;
		
	}
	
	
	/**
	 * Get facebook URL from an entity
	 * #!ArticleSection:1 -> "/fb/ArticleSection/1"
	 * @param url
	 * @return
	 */
	public static String convertToFacebookUrl(ArticleSectionProxy entityBase){
		
		String url = AppUtilsShared.DOMAIN_PROD + FACEBOOK_MARKER + "/ArticleSection"+"/"+entityBase.getId();
		
		return url;
		
	}
	
	/**
	 * Generate relative URL in the form "#!ArticleSection:1"
	 * @param entityName
	 * @param id
	 * @return
	 */
	public static String generateGWTUrl(String entityName, Long id){
		
		return DOMAIN_PROD+hashFragment + entityName + ":" +id;
		
	}
	
	public static String generateRandomNumber(){
		// TODO create better random generator
		return String.valueOf(new Random().nextDouble()*10000000).replace(".", "-");
	}
	
	/**
	 * Check if the given string is null or empty
	 * @param string
	 * @return true, if given string is null or empty
	 */
	public static boolean isNullOrEmpty(String string){
		
		if(string == null || "".equals(string))
			return true;
		
		return false;
		
	}
	
	
	
}
